package com.fly.estore.controller;

import com.fly.estore.domain.*;
import com.fly.estore.framework.common.AjaxResult;
import com.fly.estore.framework.session.AccountSession;
import com.fly.estore.framework.session.support.SessionInfo;
import com.fly.estore.framework.util.CommonUtil;
import com.fly.estore.service.MemberService;
import com.fly.estore.service.ProductService;
import com.fly.estore.service.SaleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Fly on 2016/4/28.
 */
@Controller
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    SaleService saleService;
    @Autowired
    ProductService productService;
    @Autowired
    MemberService memberService;

    @RequestMapping("/order/list")
    public String saleOrder(
            ModelMap model
    ){
        List<SaleInfo> saleList = saleService.getSaleList();
        model.addAttribute("saleList", saleList);
        return "/sale/info/list";
    }

    @RequestMapping("/order/info")
    public String saleInfo(
            @RequestParam(value = "saleId", required = false, defaultValue = "")String saleId,
            ModelMap model
    ){
        SaleInfo sale = new SaleInfo();
        if (StringUtils.isNotBlank(saleId)){
            sale = saleService.getSaleById(saleId);
        }
        model.addAttribute("sale", sale);
        List<MemberInfo> memberList = memberService.queryMemberList("");
        model.addAttribute("memberList", memberList);
        return "/sale/info/detail";
    }

    @RequestMapping("/order/saveSale")
    @ResponseBody
    public AjaxResult saveSale(
            @RequestParam(value = "saleId", required = false, defaultValue = "")String saleId,
            @RequestParam(value = "memberId", required = false, defaultValue = "")String memberId,
            @RequestParam(value = "payPrice", required = false, defaultValue = "0")float payPrice,
            @RequestParam(value = "totalPrice", required = false, defaultValue = "0")float totalPrice,
            @SessionInfo AccountSession accountSession
            ){
        SaleInfo sale = new SaleInfo();
        sale.setSaleId(saleId);
        sale.setPayPrice(payPrice);
        sale.setTotalPrice(totalPrice);
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setMemberId(memberId);
        sale.setMember(memberInfo);
        sale.setCreateId(accountSession.getAccountId());
        sale.setCreateTime(CommonUtil.currentTime());
        sale.setLastUpdateId(accountSession.getAccountId());
        sale.setLastUpdateTime(CommonUtil.currentTime());
        String newSaleId = CommonUtil.genPK();
        int update = saleService.saveSale(sale, newSaleId);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        result.setData(StringUtils.isEmpty(saleId) ? newSaleId : saleId);
        return result;
    }

    @RequestMapping("/order/confirm")
    @ResponseBody
    public AjaxResult confirmSale(
            @RequestParam("saleId")String saleId,
            @RequestParam("cutrate")float cutrate
    ){
        int update = saleService.confirmSale(saleId, cutrate);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/order/deleteSale")
    @ResponseBody
    public AjaxResult deleteSale(
            @RequestParam("saleId")String saleId
    ){
        int update = saleService.deleteSaleById(saleId);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/order/detail")
    public String saleDetail(
            @RequestParam(value = "detailId", required = false, defaultValue = "")String saleDetailId,
            @RequestParam(value = "memberId", required = false, defaultValue = "")String memberId,
            @RequestParam(value = "saleId", required = false, defaultValue = "")String saleId,
            ModelMap model
    ){
        SaleDetail detail = new SaleDetail();
        if (StringUtils.isNotBlank(saleDetailId)){
            detail = saleService.getDetailById(saleDetailId);
        }
        model.addAttribute("detail", detail);
        MemberInfo memberInfo = memberService.getById(memberId);
        model.addAttribute("member", memberInfo);
//        List<ProductInfo> productList = productService.getProductList();
//        model.addAttribute("productList", productList);
//        List<ProductCatalog> catalogList = productService.getCatalogList();
//        model.addAttribute("catalogList", catalogList);
        model.addAttribute("saleId", saleId);
        return "/sale/info/saleDetail";
    }

    @RequestMapping("/order/saveDetail")
    @ResponseBody
    public AjaxResult saveDetail(
            @RequestParam(value = "detailId", required = false, defaultValue = "")String saleDetailId,
            @RequestParam(value = "saleId", required = false, defaultValue = "")String saleId,
            @RequestParam(value = "catalogId", required = false, defaultValue = "")String catalogId,
            @RequestParam(value = "productId", required = false, defaultValue = "")String productId,
            @RequestParam(value = "productSkuId",required = false, defaultValue = "")String productSkuId,
            @RequestParam(value = "amount", required = false, defaultValue = "0")int amount,
            @RequestParam(value = "price", required = false, defaultValue = "0")float price,
            @RequestParam(value = "memberId", required = false, defaultValue = "")String memberId
    ){
        SaleDetail saleDetail = new SaleDetail();
        saleDetail.setSaleDetailId(saleDetailId);
        saleDetail.setSaleId(saleId);
        ProductCatalog catalog = productService.getCatalogById(catalogId);
        ProductInfo product = new ProductInfo();
        product.setProductId(productId);
        saleDetail.setProduct(product);
        ProductSKU sku = new ProductSKU();
        sku.setProductSkuId(productSkuId);
        saleDetail.setProductSKU(sku);
        saleDetail.setAmount(amount);
        MemberInfo member = memberService.getById(memberId);
        if (catalog.getDiscount().equals(ProductCatalog.DISCOUNT_YES) && member.getMemberLevel().getDiscount() > 0){
            saleDetail.setPrice(amount * price * (member.getMemberLevel().getDiscount() / 100));
        }
        else {
            saleDetail.setPrice(amount * price);
        }
        int update = saleService.saveSaleDetail(saleDetail);

        //更新订单总价
        if (update > 0){
            saleService.updateSalePrice(saleId);
        }

        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/order/deleteDetail")
    @ResponseBody
    public AjaxResult deleteDetail(
            @RequestParam(value = "detailId", required = false, defaultValue = "")String saleDetailId,
            @RequestParam(value = "saleId", required = false, defaultValue = "")String saleId
    ){
        int update = saleService.deleteDetailById(saleDetailId);
        if (update > 0){
            saleService.updateSalePrice(saleId);
        }

        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }
}
